package xyz.pro5books.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.pro5books.pojo.Book;
import xyz.pro5books.pojo.PaymentInfo;
import xyz.pro5books.service.BookService;
import xyz.pro5books.vo.PageResult;
import xyz.pro5books.vo.PageResult2;
import xyz.pro5books.vo.SysResult;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/getPaymentInfo")
    public SysResult getPaymentInfo(String token){
        PaymentInfo paymentInfo = bookService.getPaymentInfo(token);
        if (paymentInfo != null){
            return SysResult.success(paymentInfo);
        }else {
            return SysResult.fail();
        }
    }

    @GetMapping("/getOnlineHtml/{bookId}")
    public String getOnlineHtml(@PathVariable Integer bookId){
        return "<h1>fantasticWeb</h1><h1><span>0 公用对象</span></h1><h2 ><span>0.0 响应的SysResult对象：</span></h2>";
    }

    @PutMapping("/download/{bookId}/{downNum}")
    public SysResult addDownloadNum(@PathVariable Integer bookId, @PathVariable Integer downNum){
        bookService.addDownloadNum(bookId, downNum);
        return SysResult.success();
    }

    @GetMapping("/searchall")
    public SysResult searchall(PageResult2 pageResult2){
        return SysResult.success(bookService.searchall(pageResult2));
    }

    @GetMapping("/getmenulist")
    public SysResult getmenulist(){
        return SysResult.success(bookService.getmenulist());
    }

    @GetMapping("/searchall/{id}")
    public SysResult getBookDetail(@PathVariable Integer id){
        return SysResult.success(bookService.getBookById(id));
    }

    @PutMapping("/collections/{userId}/{bookId}/{collType}")
    public SysResult addCollections(@PathVariable Integer userId, @PathVariable Integer bookId, @PathVariable Integer collType){
        if (collType == 1){
            bookService.addCollections(userId,bookId);
        }
        else {
            bookService.concelCollect(userId,bookId);
        }
        return SysResult.success();
    }

    @GetMapping("/getBookCollectState/{userId}/{bookId}")
    public SysResult getBookCollectState(@PathVariable Integer userId, @PathVariable Integer bookId){
        Integer bookCollectState = bookService.getBookCollectState(userId, bookId);
        return SysResult.success(bookCollectState);
    }

    @GetMapping("/getUserInfo/{userId}")
    public SysResult getUserInfo(@PathVariable Integer userId){
        Integer userCollNum = bookService.getUserCollNum(userId);
        return SysResult.success(userCollNum);
    }

    @GetMapping("/getCollList/{userId}")
    public SysResult getCollList(@PathVariable Integer userId){
        return SysResult.success(bookService.getCollList(userId));
    }

    @PutMapping("/report/{userId}/{bookId}/{reason}")
    public SysResult doReport(@PathVariable Integer userId, @PathVariable Integer bookId, @PathVariable Integer reason){
        bookService.doReport(userId, bookId, reason);
        return SysResult.success();
    }

    @PutMapping("/report/{userId}/{bookId}")
    public SysResult concelReport(@PathVariable Integer userId, @PathVariable Integer bookId){
        bookService.concelReport(userId,bookId);
        return SysResult.success();
    }

    @GetMapping("/getBookPic/{bookId}")
    public SysResult getBookPic(@PathVariable Integer bookId){
        return SysResult.success(bookService.getBookPic(bookId));
    }

    @GetMapping("/getBookReportState/{userId}/{bookId}")
    public SysResult getBookReportState(@PathVariable Integer userId, @PathVariable Integer bookId){
        Integer bookReportState = bookService.getBookReportState(userId, bookId);
        return SysResult.success(bookReportState);
    }

    @GetMapping("/test")
    public String TestString(){
        return "helloWorld";
    }

    //获取图书列表
    @GetMapping("/getBookList")
    public SysResult getBookList(PageResult pageResult){
        pageResult = bookService.getBookList(pageResult);
        return SysResult.success(pageResult);
    }
    //修改图书状态
    @PutMapping("/updateBookStatus/{id}/{status}")
    public SysResult updateBookStatus(Book book){
        bookService.updateBookStatus(book);
        return SysResult.success();
    }
    //删除图书
    @DeleteMapping("/deleteBookById")
    public SysResult deleteBook(Integer id){
        bookService.deleteBook(id);
        return SysResult.success();
    }
    //获取要修改的图书的信息
    @GetMapping("/{id}")
    public SysResult getBook(@PathVariable Integer id){
        Book book = bookService.getBook(id);
        return SysResult.success(book);
    }
    //修改图书信息
    @PutMapping("/updateBook")
    public SysResult updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return SysResult.success();
    }
    //书籍上传数量统计
    @GetMapping("/bookCount")
    public SysResult selBookCount(){
        List list = bookService.selBookCount();
        return SysResult.success(list);
    }


}
